#!/bin/bash
# scripts/manage_services.sh

# 定义 PID 文件路径
GATEWAY_PID_FILE="runtime/pids/gateway.pid"
PROXY_PID_FILE="runtime/pids/proxy.pid"

# 强制杀死占用指定端口的进程
kill_port_processes() {
    local port=$1
    local pids=$(lsof -ti:$port)
    if [ ! -z "$pids" ]; then
        echo "Killing processes on port $port: $pids"
        kill -9 $pids 2>/dev/null
    fi
}

# 等待端口释放
wait_for_port_release() {
    local port=$1
    local timeout=${2:-10}
    local count=0
    
    while lsof -i :$port >/dev/null 2>&1 && [ $count -lt $timeout ]; do
        echo "Waiting for port $port to be released..."
        sleep 1
        count=$((count + 1))
    done
    
    if [ $count -ge $timeout ]; then
        echo "Timeout waiting for port $port to be released"
        return 1
    fi
    
    echo "Port $port has been released"
    return 0
}

case "$1" in
    start)
        echo "Starting backend services..."
        uv run scripts/start_services.py
        
        # 等待后端服务启动
        sleep 3
        
        echo "Starting API gateway..."
        # 在后台启动网关服务
        uv run scripts/start_gateway.py > runtime/logs/gateway.log 2>&1 &
        GATEWAY_PID=$!
        echo $GATEWAY_PID > $GATEWAY_PID_FILE
        echo "API Gateway started with PID $GATEWAY_PID"
        
        echo "Starting OpenAI compatible proxy..."
        # 在后台启动清洗代理服务
        uv run scripts/start_clean_proxy.py > runtime/logs/proxy.log 2>&1 &
        PROXY_PID=$!
        echo $PROXY_PID > $PROXY_PID_FILE
        echo "OpenAI Compatible Proxy started with PID $PROXY_PID"
        ;;
    stop)
        echo "Stopping OpenAI compatible proxy..."
        if [ -f $PROXY_PID_FILE ]; then
            PROXY_PID=$(cat $PROXY_PID_FILE)
            if kill -0 $PROXY_PID 2>/dev/null; then
                kill $PROXY_PID
                # 等待进程完全终止
                wait_for_port_release 8003 15
            else
                echo "OpenAI Compatible Proxy not running"
            fi
            rm -f $PROXY_PID_FILE
        else
            echo "OpenAI Compatible Proxy PID file not found"
        fi
        
        echo "Stopping API gateway..."
        if [ -f $GATEWAY_PID_FILE ]; then
            GATEWAY_PID=$(cat $GATEWAY_PID_FILE)
            if kill -0 $GATEWAY_PID 2>/dev/null; then
                kill $GATEWAY_PID
                # 等待进程完全终止
                wait_for_port_release 8000 15
            else
                echo "API Gateway not running"
            fi
            rm -f $GATEWAY_PID_FILE
        else
            echo "API Gateway PID file not found"
        fi
        
        echo "Stopping backend services..."
        uv run scripts/stop_services.py
        
        # 强制杀死可能残留的进程
        kill_port_processes 8001
        kill_port_processes 8002
        
        # 等待端口释放
        wait_for_port_release 8001 10
        wait_for_port_release 8002 10
        ;;
    restart)
        echo "Restarting services..."
        $0 stop
        sleep 2
        $0 start
        ;;
    status)
        echo "Checking service status..."
        if lsof -i :8001 >/dev/null 2>&1; then
            echo "Embedding service (port 8001): Running"
        else
            echo "Embedding service (port 8001): Not running"
        fi
        
        if lsof -i :8002 >/dev/null 2>&1; then
            echo "Reranker service (port 8002): Running"
        else
            echo "Reranker service (port 8002): Not running"
        fi
        
        if [ -f $GATEWAY_PID_FILE ]; then
            GATEWAY_PID=$(cat $GATEWAY_PID_FILE)
            if kill -0 $GATEWAY_PID 2>/dev/null; then
                echo "API Gateway (port 8000): Running (PID: $GATEWAY_PID)"
            else
                echo "API Gateway (port 8000): Not running"
            fi
        else
            echo "API Gateway (port 8000): Not running"
        fi
        
        if [ -f $PROXY_PID_FILE ]; then
            PROXY_PID=$(cat $PROXY_PID_FILE)
            if kill -0 $PROXY_PID 2>/dev/null; then
                echo "OpenAI Compatible Proxy (port 8003): Running (PID: $PROXY_PID)"
            else
                echo "OpenAI Compatible Proxy (port 8003): Not running"
            fi
        else
            echo "OpenAI Compatible Proxy (port 8003): Not running"
        fi
        ;;
    kill)
        echo "Force killing all service processes..."
        # 强制杀死所有相关端口的进程
        kill_port_processes 8000
        kill_port_processes 8001
        kill_port_processes 8002
        kill_port_processes 8003
        
        # 删除 PID 文件
        rm -f $GATEWAY_PID_FILE $PROXY_PID_FILE runtime/pids/*.pid
        
        # 等待端口释放
        wait_for_port_release 8000 10
        wait_for_port_release 8001 10
        wait_for_port_release 8002 10
        wait_for_port_release 8003 10
        
        echo "All services force killed"
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status|kill}"
        exit 1
        ;;
esac